<template>
  <view
    class="fridge-settings"
    :class="{
      'is-off-line': control.store.isOffline,
    }"
  >
    <SimpleSettingCard
      v-for="(setting, index) in isVirtualAppliance ? settingGroups : newSettingGroups"
      :key="index"
      :name="(setting.name as string)"
      :value="(setting.value as string | number)"
      :unit="setting.unit"
      :setting-key="setting.key"
      @click="openSetting(setting)"
    />
  </view>

  <Modal
    :visible="isShowTips"
    :title="$t('FridgeFreezer.FlexCompartment.SetpointTemperature.title.Text')"
    @ok="isShowTips = false"
    ok-text="知道了"
  >
    <view class="SetpointTemperature-tips">
      {{ $t('FridgeFreezer.FlexCompartment.SetpointTemperature.tips.Text') }}
    </view>
  </Modal>
</template>

<script setup lang="ts">
import { getImagesGroup } from '@/api/fridge'
import Modal from '@/common/modal.vue'
import {
  FLEX_KEY,
  FLEX_MODE_KEY,
  REFRIGERATION_CHILLER_COMMON_SETPOINT_TEMPERATURE,
  SMART_HYGIENE_BOOST,
  SETPOINT_TEMPERATURE_FREEZER,
  SETPOINT_TEMPERATURE_FREEZER2,
  SETPOINT_TEMPERATURE_REFRIGERATOR,
  SETPOINT_TEMPERATURE_REFRIGERATOR2,
  SMART_HYGIENE_ENABLED,
  SMART_HYGIENE_CLOSED,
  SMART_HYGIENE_MODE_CLOSE,
  SMART_HYGIENE_MODE_AUTO,
  SMART_HYGIENE_MODE_MANUAL,
  SETTING_ENABLECAMERAS,
  SETTING_PRESET,
  SETPOINT_TEMPERATURE_CHILLERLEFT,
} from '@/constants/Command'
import { FLEXMDOES } from '@/controller/FridgeFreezer'
import type { FridgeFreezerController, FridgeSettingGroup } from '@/controller/FridgeFreezer/definition'
import SimpleSettingCard from '../components/simpleSettingCard.vue'
import { TrackType, trackEvent } from '@/dataReport/tracks'
import { safeTrack } from '@/dataReport/dataReportUtil'
const props = defineProps<{
  control: FridgeFreezerController.Control
}>()
const emit = defineEmits<{
  (e: 'open-setting', setting: FridgeSettingGroup): void
  (e: 'open-modal', setting: FridgeSettingGroup): void
}>()
const sortKey = [
  SMART_HYGIENE_ENABLED,
  SETPOINT_TEMPERATURE_REFRIGERATOR,
  SETPOINT_TEMPERATURE_REFRIGERATOR2,
  SETTING_PRESET,
  SETPOINT_TEMPERATURE_CHILLERLEFT,
  FLEX_KEY,
  FLEX_MODE_KEY,
  REFRIGERATION_CHILLER_COMMON_SETPOINT_TEMPERATURE,
  SETPOINT_TEMPERATURE_FREEZER,
  SETPOINT_TEMPERATURE_FREEZER2,
  SMART_HYGIENE_CLOSED,
  SMART_HYGIENE_BOOST,
  'ModeChoose',
  SMART_HYGIENE_MODE_CLOSE,
  SMART_HYGIENE_MODE_AUTO,
  SMART_HYGIENE_MODE_MANUAL,
  SETTING_ENABLECAMERAS,
]

/**
 * 冰箱设置
 */
const settingGroups = computed(() => props.control.store.settingGroups)
const isVirtualAppliance = computed(() => props.control.store.isVirtualAppliance)
const newSettingGroups = ref<FridgeSettingGroup[]>([])
const smartHygienMode = computed(() => props.control.store.smartHygieneMode)
const smartHygieneLiteMode = computed(() => props.control.store.smartHygieneLiteMode)
const isSmartHygieneLite = computed(() => props.control.store.isSmartHygieneLite)

/**
 * 排序
 */
watch(
  () => settingGroups.value,

  (settingGroups) => {
    if (!settingGroups || settingGroups.length === 0) return
    newSettingGroups.value = []
    sortKey.forEach(async (item) => {
      let curIndex = settingGroups.findIndex((setting) => setting.key === item)
      if (curIndex !== -1 && newSettingGroups.value.length < settingGroups.length) {
        let curSetting = settingGroups.find((setting) => setting.key === item) as FridgeSettingGroup
        // 摄像头特殊处理
        if (curSetting.key === SETTING_ENABLECAMERAS) {
          const res = await getImagesGroup(props.control.store._datasource.haId)
          if (curSetting.value === '暂未开启' || res.data.images.length === 0) {
            return
          } else {
            let filterCheck = newSettingGroups.value.findIndex((items) => items.key === curSetting.key)
            if (filterCheck === -1) {
              newSettingGroups.value.push(curSetting)
            }
          }
        } else {
          newSettingGroups.value.push(curSetting)
        }
        newSettingGroups.value.map((items, index) => {
          if (items.key == SMART_HYGIENE_ENABLED && isSmartHygieneLite.value) {
            if (
              smartHygienMode.value === SMART_HYGIENE_MODE_CLOSE ||
              smartHygieneLiteMode.value === SMART_HYGIENE_MODE_CLOSE
            ) {
              items.value = '关'
            }
            if (
              smartHygienMode.value === SMART_HYGIENE_MODE_AUTO ||
              smartHygieneLiteMode.value === SMART_HYGIENE_MODE_AUTO
            ) {
              items.value = '自动'
            }
            if (
              smartHygienMode.value === SMART_HYGIENE_MODE_MANUAL ||
              smartHygieneLiteMode.value === SMART_HYGIENE_MODE_MANUAL
            ) {
              items.value = '强力杀菌'
            }
          }
          if (items.key == FLEX_KEY) {
            FLEXMDOES.map((flexOptions) => {
              if (items.value == flexOptions.value) {
                items.value =
                  props.control.store._datasource.brand == 'SIEMENS' ? flexOptions.SIEMENS_name : flexOptions.BOSCH_name
                items.unit = ''
              }
            })
          }
        })
      }
    })
    // 列表筛选处理
    // newSettingGroups.value.filter(async (items, index, self) => {
    //   if (items.key === SETTING_ENABLECAMERAS) {
    //
    //     const res = await getImagesGroup(props.control.store._datasource.haId)
    //     if (items.value === '暂未开启' || res.data.images.length === 0) {
    //       self.splice(index, 1)
    //     }
    //   }
    // })
  }
)
/**
 * 设置零度弹窗
 */
const isShowTips = ref(false)

function openTips() {
  isShowTips.value = true
}

/**
 * 打开冰箱setting设置弹框
 */
async function openSetting(setting: FridgeSettingGroup) {
  safeTrack(() => {
    let trackKey = setting.key
    trackEvent(trackKey ? trackKey : 'openSetting', setting, TrackType.ApplianceDetail)
  })
  if (setting.key !== SETTING_ENABLECAMERAS) {
    emit('open-setting', setting)
  } else {
    emit('open-modal', setting)
  }
}
</script>

<style scoped lang="less">
.fridge-settings {
  padding: 0 32rpx;
}
.is-off-line {
  pointer-events: none;
  opacity: 0.6;
}
</style>
